﻿using System;
using System.Text;
namespace TicTacToe
{
    public interface IBoardPresenter{
        String PresentBoard(CGame game);
    }
    public class BoardPresenter : IBoardPresenter
    {
        static private BoardPresenter singalton = null;
        static public BoardPresenter Instance(){
            if (singalton == null){
                singalton = new BoardPresenter();
            }
            return singalton;
        }
        private BoardPresenter(){ }
        //这种表示方法是和一个叫做 Mysql 的毒瘤软件学的。
        Char AngelPrel = '+';
        Char HorizonDelimiter = '-';
        Char VerticalDelimiter = '|';
        virtual public String PresentBoard(CGame game){
            StringBuilder SB = new StringBuilder();
            for (int x = 0; x < CGame.boardSize; x++)
            {
                for (int y = 0; y < CGame.boardSize; y++)
                {
                    SB.Append(AngelPrel);
                    SB.Append(new String(HorizonDelimiter, game.GetChessBoard(x, y).Repl.Length));
                }
                SB.Append(AngelPrel);
                SB.Append('\n');
                for (int y = 0; y < CGame.boardSize; y++)
                {
                    SB.Append(VerticalDelimiter);
                    SB.Append(game.GetChessBoard(x, y).Repl);
                }
                SB.Append(VerticalDelimiter);
                SB.Append('\n');
            }
            for (int y = 0; y < CGame.boardSize; y++)
            {
                SB.Append(AngelPrel);
                SB.Append(new String(HorizonDelimiter, game.GetChessBoard(CGame.boardSize - 1, y).Repl.Length));
            }
            SB.Append(AngelPrel);
            SB.Append('\n');
            return SB.ToString();
        }
    }
}
